import sys
sys.path.append('D:/Anaconda/Library/bin')
import gc
import threading
import tkinter as tk
from tkinter import filedialog
from MainWindow import Ui_MainWindow

from PyQt5 import QtCore, QtGui, QtWidgets
import os
import numpy as np
import datetime
import collections

from PyQt5.QtCore import pyqtSignal
from PyQt5.QtGui import *
from PyQt5.QtWidgets import QDialog, QMessageBox, QPushButton, QTableView, QLabel, QHeaderView, QVBoxLayout

from Scenic_mutate import mutate_process, coordinate_mutate, angle_mutate, map_mutate
from Test_autodrive import get_result, get_result_all, cal_result_trans_all_new, get_meta_values, get_result_deeptest, \
    cal_result_trans_all_deeptest, get_deeptest_values, cal_result_trans_coverage, cal_result_trans
from deeptest_mutate import deeptest
from threading import Thread

class mywindow(QtWidgets.QMainWindow, Ui_MainWindow):
    signal = pyqtSignal(dict, str)
    all_signal = pyqtSignal(list, list)
    def __init__(self):
        super(mywindow, self).__init__()
        self.setupUi(self)
# 获得原始场景的渲染结果和预测结果
    def original_render(self):
        starttime = datetime.datetime.now()
        lines = self.lineEdit_original.text().split('/')
        filepath1 = ''
        filepath2 = ''
        for i in range(len(lines)):
            if i == 0:
                filepath1 = lines[i]
            else:
                filepath1 = filepath1 + '/' + lines[i]
        # filepath1 = filepath1 + '/'
        lines = self.lineEdit_original.text().split('/')
        for i in range(len(lines)-2):
            if i == 0:
                filepath2 = lines[i]
            else:
                filepath2 = filepath2 + '/' + lines[i]
        filepath2 = filepath2 + '/'
        # print(self.lineEdit.text())
        # print('**************************************************************************************************************')
        print(filepath1, filepath2)
        mutate_process(filepath1, 1, filepath2+'original')
        get_result(filepath2+'original')
        #change_weight_(filepath1, filepath2)
        # print(self.lineEdit)
        # neural_test('F:\\fault_localization\\test')
        # print(
        # '**************************************************************************************************************')
        # if a == 0:
        endtime = datetime.datetime.now()
        seconds = (endtime - starttime).seconds
        start = starttime.strftime('%Y-%m-%d %H:%M')
        # 100 秒
        # 分钟
        minutes = seconds // 60
        second = seconds % 60
        print((endtime - starttime))
        timeStr = str(minutes) + '分钟' + str(second) + "秒"
        print("程序从 " + start + ' 开始运行,运行时间为：' + timeStr)
        reply = QMessageBox.information(self, "提示", "初始场景渲染并预测完成,用时" + timeStr, QMessageBox.Yes | QMessageBox.No)
    def mr1(self):
        starttime = datetime.datetime.now()
        lines = self.lineEdit_original.text().split('/')
        filepath1 = ''
        filepath2 = ''
        for i in range(len(lines)):
            if i == 0:
                filepath1 = lines[i]
            else:
                filepath1 = filepath1 + '/' + lines[i]
        # filepath1 = filepath1 + '\\'
        lines = self.lineEdit_original.text().split('/')
        for i in range(len(lines)-2):
            if i == 0:
                filepath2 = lines[i]
            else:
                filepath2 = filepath2 + '/' + lines[i]
        filepath2 = filepath2 + '/'
        # print(self.lineEdit.text())
        # print('**************************************************************************************************************')
        print(filepath1, filepath2)
        mutate_process(filepath1, 20, filepath2+'mutants_mr1')
        #change_weight_(filepath1, filepath2)
        # print(self.lineEdit)
        # neural_test('F:\\fault_localization\\test')
        # print(
        # '**************************************************************************************************************')
        # if a == 0:
        endtime = datetime.datetime.now()
        seconds = (endtime - starttime).seconds
        start = starttime.strftime('%Y-%m-%d %H:%M')
        # 100 秒
        # 分钟
        minutes = seconds // 60
        second = seconds % 60
        print((endtime - starttime))
        timeStr = str(minutes) + '分钟' + str(second) + "秒"
        print("程序从 " + start + ' 开始运行,运行时间为：' + timeStr)
        reply = QMessageBox.information(self, "提示", "MR1变异完成,用时" + timeStr, QMessageBox.Yes | QMessageBox.No)
    def mr2(self):
        starttime = datetime.datetime.now()
        lines = self.lineEdit_original.text().split('/')
        filepath1 = ''
        filepath2 = ''
        for i in range(len(lines)):
            if i == 0:
                filepath1 = lines[i]
            else:
                filepath1 = filepath1 + '\\' + lines[i]
        # filepath1 = filepath1 + '\\'
        lines = self.lineEdit_original.text().split('/')
        for i in range(len(lines)-2):
            if i == 0:
                filepath2 = lines[i]
            else:
                filepath2 = filepath2 + '\\' + lines[i]
        filepath2 = filepath2 + '\\'
        # print(self.lineEdit.text())
        # print('**************************************************************************************************************')
        print(filepath1, filepath2)
        coordinate_mutate(filepath1)
        #mutate_process(filepath1, 20, filepath2+'mutants_mr1/')
        #change_weight_(filepath1, filepath2)
        # print(self.lineEdit)
        # neural_test('F:\\fault_localization\\test')
        # print(
        # '**************************************************************************************************************')
        # if a == 0:
        endtime = datetime.datetime.now()
        seconds = (endtime - starttime).seconds
        start = starttime.strftime('%Y-%m-%d %H:%M')
        # 100 秒
        # 分钟
        minutes = seconds // 60
        second = seconds % 60
        print((endtime - starttime))
        timeStr = str(minutes) + '分钟' + str(second) + "秒"
        print("程序从 " + start + ' 开始运行,运行时间为：' + timeStr)
        reply = QMessageBox.information(self, "提示", "MR2变异完成,用时" + timeStr, QMessageBox.Yes | QMessageBox.No)
    def mr3(self):
        starttime = datetime.datetime.now()
        lines = self.lineEdit_original.text().split('/')
        filepath1 = ''
        filepath2 = ''
        for i in range(len(lines)):
            if i == 0:
                filepath1 = lines[i]
            else:
                filepath1 = filepath1 + '\\' + lines[i]
        # filepath1 = filepath1 + '\\'
        lines = self.lineEdit_original.text().split('/')
        for i in range(len(lines)-2):
            if i == 0:
                filepath2 = lines[i]
            else:
                filepath2 = filepath2 + '\\' + lines[i]
        filepath2 = filepath2 + '\\'
        # print(self.lineEdit.text())
        # print('**************************************************************************************************************')
        print(filepath1, filepath2)
        angle_mutate(filepath1)
        #mutate_process(filepath1, 20, filepath2+'mutants_mr1/')
        #change_weight_(filepath1, filepath2)
        # print(self.lineEdit)
        # neural_test('F:\\fault_localization\\test')
        # print(
        # '**************************************************************************************************************')
        # if a == 0:
        endtime = datetime.datetime.now()
        seconds = (endtime - starttime).seconds
        start = starttime.strftime('%Y-%m-%d %H:%M')
        # 100 秒
        # 分钟
        minutes = seconds // 60
        second = seconds % 60
        print((endtime - starttime))
        timeStr = str(minutes) + '分钟' + str(second) + "秒"
        print("程序从 " + start + ' 开始运行,运行时间为：' + timeStr)
        reply = QMessageBox.information(self, "提示", "MR3变异完成,用时" + timeStr, QMessageBox.Yes | QMessageBox.No)
    def mr4(self):
        starttime = datetime.datetime.now()
        lines = self.lineEdit_original.text().split('/')
        filepath1 = ''
        filepath2 = ''
        for i in range(len(lines)):
            if i == 0:
                filepath1 = lines[i]
            else:
                filepath1 = filepath1 + '\\' + lines[i]
        # filepath1 = filepath1 + '\\'
        lines = self.lineEdit_original.text().split('/')
        for i in range(len(lines)-2):
            if i == 0:
                filepath2 = lines[i]
            else:
                filepath2 = filepath2 + '\\' + lines[i]
        filepath2 = filepath2 + '\\'
        # print(self.lineEdit.text())
        # print('**************************************************************************************************************')
        print(filepath1, filepath2)
        map_mutate(filepath1)
        #mutate_process(filepath1, 20, filepath2+'mutants_mr1/')
        #change_weight_(filepath1, filepath2)
        # print(self.lineEdit)
        # neural_test('F:\\fault_localization\\test')
        # print(
        # '**************************************************************************************************************')
        # if a == 0:
        endtime = datetime.datetime.now()
        seconds = (endtime - starttime).seconds
        start = starttime.strftime('%Y-%m-%d %H:%M')
        # 100 秒
        # 分钟
        minutes = seconds // 60
        second = seconds % 60
        print((endtime - starttime))
        timeStr = str(minutes) + '分钟' + str(second) + "秒"
        print("程序从 " + start + ' 开始运行,运行时间为：' + timeStr)
        reply = QMessageBox.information(self, "提示", "MR4变异完成,用时" + timeStr, QMessageBox.Yes | QMessageBox.No)
    def DeepTest(self):
        starttime = datetime.datetime.now()
        lines = self.lineEdit_original.text().split('/')
        filepath1 = ''
        filepath2 = ''
        for i in range(len(lines)):
            if i == 0:
                filepath1 = lines[i]
            else:
                filepath1 = filepath1 + '\\' + lines[i]
       # filepath1 = filepath1 + '\\'
        lines = self.lineEdit_original.text().split('/')
        for i in range(len(lines)-3):
            if i == 0:
                filepath2 = lines[i]
            else:
                filepath2 = filepath2 + '\\' + lines[i]
        filepath2 = filepath2 + '\\'
        # print(self.lineEdit.text())
        # print('**************************************************************************************************************')
        print(filepath1, filepath2)
        deeptest(filepath1,filepath2+'deep_test/')
        #mutate_process(filepath1, 20, filepath2+'mutants_mr1/')
        #change_weight_(filepath1, filepath2)
        # print(self.lineEdit)
        # neural_test('F:\\fault_localization\\test')
        # print(
        # '**************************************************************************************************************')
        # if a == 0:
        endtime = datetime.datetime.now()
        seconds = (endtime - starttime).seconds
        start = starttime.strftime('%Y-%m-%d %H:%M')
        # 100 秒
        # 分钟
        minutes = seconds // 60
        second = seconds % 60
        print((endtime - starttime))
        timeStr = str(minutes) + '分钟' + str(second) + "秒"
        print("程序从 " + start + ' 开始运行,运行时间为：' + timeStr)
        reply = QMessageBox.information(self, "提示", "DeepTest变异完成,用时" + timeStr, QMessageBox.Yes | QMessageBox.No)
    def equal_verify(self):
        starttime = datetime.datetime.now()
        lines = self.lineEdit_verify.text().split('/')
        filepath1 = ''
        filepath2 = ''
        for i in range(len(lines)):
            if i == 0:
                filepath1 = lines[i]
            else:
                filepath1 = filepath1 + '\\' + lines[i]
        # filepath1 = filepath1 + '\\'
        lines = self.lineEdit_verify.text().split('/')
        for i in range(len(lines) - 1):
            if i == 0:
                filepath2 = lines[i]
            else:
                filepath2 = filepath2 + '\\' + lines[i]
        filepath2 = filepath2 + '\\'
        # print(self.lineEdit.text())
        # print('**************************************************************************************************************')
        print(filepath1, filepath2)
        if 'mutants_mr1' in lines:
            get_result(filepath1+'screenShot')
            cal_result_trans(filepath2+'original',filepath1,0.4)
            cal_result_trans_coverage(filepath2+'original',filepath1)
        else:
            get_result_all(filepath1)
            cal_result_trans_all_new(filepath2+'original',filepath1,0.4)
        get_meta_values(filepath1)
        endtime = datetime.datetime.now()
        seconds = (endtime - starttime).seconds
        start = starttime.strftime('%Y-%m-%d %H:%M')
        # 100 秒
        # 分钟
        minutes = seconds // 60
        second = seconds % 60
        print((endtime - starttime))
        timeStr = str(minutes) + '分钟' + str(second) + "秒"
        print("程序从 " + start + ' 开始运行,运行时间为：' + timeStr)
        reply = QMessageBox.information(self, "提示", "蜕变关系验证完成,用时" + timeStr, QMessageBox.Yes | QMessageBox.No)
    def deeptest_verify(self):
        starttime = datetime.datetime.now()
        lines = self.lineEdit_verify.text().split('/')
        filepath1 = ''
        filepath2 = ''
        for i in range(len(lines)):
            if i == 0:
                filepath1 = lines[i]
            else:
                filepath1 = filepath1 + '\\' + lines[i]
        #filepath1 = filepath1 + '\\'
        lines = self.lineEdit_verify.text().split('/')
        for i in range(len(lines) - 2):
            if i == 0:
                filepath2 = lines[i]
            else:
                filepath2 = filepath2 + '\\' + lines[i]
        filepath2 = filepath2 + '\\'
        # print(self.lineEdit.text())
        # print('**************************************************************************************************************')
        print(filepath1, filepath2)
        get_result_deeptest(filepath1)
        cal_result_trans_all_deeptest(filepath2+'original', filepath1)
        get_deeptest_values(filepath1)
        endtime = datetime.datetime.now()
        seconds = (endtime - starttime).seconds
        start = starttime.strftime('%Y-%m-%d %H:%M')
        # 100 秒
        # 分钟
        minutes = seconds // 60
        second = seconds % 60
        print((endtime - starttime))
        timeStr = str(minutes) + '分钟' + str(second) + "秒"
        print("程序从 " + start + ' 开始运行,运行时间为：' + timeStr)
        reply = QMessageBox.information(self, "提示", "蜕变关系验证完成,用时" + timeStr, QMessageBox.Yes | QMessageBox.No)
    def setBrowerPath_orignal(self):
        root = tk.Tk()
        root.withdraw()
        file_name = filedialog.askopenfilename(title='选择文件', filetypes=[('SCENIC', '*.scenic'),('PNG', '*.png')])
        print(file_name)
        # filename = QtWidgets.QFileDialog.getOpenFileName(self, '浏览', "C:\\")
        # print(filename[0])
        # #download_path = QtWidgets.QFileDialog.getExistingDirectory(self, "浏览", "C:\\")
        self.lineEdit_original.setText(file_name)
    def setBrowerPath_verify(self):
        download_path = QtWidgets.QFileDialog.getExistingDirectory(self, "浏览", "C:\\")
        self.lineEdit_verify.setText(download_path)

    def outputWritten(self, text):  # 输出控制台信息
        cursor = self.textBrowser.textCursor()
        cursor.movePosition(QtGui.QTextCursor.End)
        cursor.insertText(text)
        self.textBrowser.setTextCursor(cursor)
        self.textBrowser.ensureCursorVisible()
def start():
    app = QtWidgets.QApplication(sys.argv)
    window = mywindow()
    window.show()
    sys.exit(app.exec_())
if __name__ == '__main__':
    # app = QtWidgets.QApplication(sys.argv)
    # window = mywindow()
    # window.show()
    # sys.exit(app.exec_())
    # mutate_process('F:/drive_scene_5_test/original_script/carla_challenge_5.scenic', 1,
    #                'F:/drive_scene_5_test/original')
    start()